$(function() {
	PubOPT.jsEventLoad(initJsEvent);
});

function initJsEvent() {
	$("input[type=checkbox]").click(function() {
		var me = $(this);
		if(me.attr("name") == 'allall'){
			$("input[type=checkbox]").attr("checked", me.attr("checked")=="checked");
		}
		else if(me.attr("name") == 'all') {
			$("input[type=checkbox][name='" + me.val() + "']").attr("checked", me.attr("checked")=="checked");
		}
		
		init();
	});
	
	$("._del").click(function() {
		var code = $(this).attr("code");
		$.jqconfirm("确认要删除该宝贝吗?", "删除宝贝", function() {
			var data = AjaxUtil.command.executeUpdate("sjc_cart_4", {goods_id: code});
			if(AjaxUtil.command.isSucceed(data) && data.rows > 0) {
				$("tr[code="+code+"]").remove();
			}
			init();
		});
	});
	
	$("._del_select").click(function() {
		if($("input[type=checkbox][code]:checked").length > 0) {
			$.jqconfirm("确认要删除这些宝贝吗？", "删除宝贝", function() {
				var arrSql = [];
				$("input[type=checkbox][code]:checked").each(function() {
					arrSql.push({sqlID:"sjc_cart_4", parameters: {goods_id: $(this).attr("code")}})
				});
				if(AjaxUtil.command.isSucceed(AjaxUtil.command.executeTransaction(arrSql))){
					$("input[type=checkbox][code]:checked").each(function() {
						$("tr[code="+$(this).attr("code")+"]").remove();
					});
				}
				init();
			});
		} else {
			$.jqalert("请选择宝贝");
		}
	});
	
	$("#account").click(function() {
		if($("input[type=checkbox][code]:checked").length <= 0) {
			return;
		}
		
		var msg = "";
		$("span ._price[dp]").each(function() {
			var me = $(this);
			if(parseFloat(me.text()) > 0) {
				if(parseFloat(me.text()) < parseFloat($("span ._lowest_price[dp="+me.attr("dp")+"]").text())) {
					var name = $("span ._shop_name[dp="+me.attr("dp")+"]").text()
					msg += (msg=="" ? name : "、"+name);
				}
			}
		});
		
		if(msg != "") {
			$.jqalert("请您确保店铺："+msg+"，达到了最低送货价格。");
			return;
		} else {
			var arrSql = [{sqlID:"sjc_cart_5"}];
			$("input[type=checkbox][code]:checked").each(function() {
				arrSql.push({sqlID:"sjc_cart_6", parameters: {goods_id: $(this).attr("code")}})
			});
			if(AjaxUtil.command.isSucceed(AjaxUtil.command.executeTransaction(arrSql))){
				location.href = basePath + "/p100026.html"; 
			}
		}
	});
	
	$("span[id^='btn_lower_']").click(function(){
		var ix = $(this).attr("id").substring(10);
		var n = strToInteger($("#num_"+ix).val(), 1);
		$("#num_"+ix).val((n>1 ? n-1 : 1));
		init();
		saveNum($("#num_"+ix).attr("code"), strToInteger($("#num_"+ix).val(), 1));
	});
	
	$("span[id^='btn_add_']").click(function(){
		var ix = $(this).attr("id").substring(8);
		var n = strToInteger($("#num_"+ix).val(), 1);
		$("#num_"+ix).val(n+1);
		init();
		saveNum($("#num_"+ix).attr("code"), strToInteger($("#num_"+ix).val(), 1));
	});
	
	init();
}

function init() {
	initState();
	initStatistical();
	updateShop();
	updateSum();
}

function updateShop() {
	$("tr:not([code])[dp]").each(function() {
		var me = $(this);
		if($("tr[code][dp=" + me.attr("dp") + "]").length <= 0) {
			me.remove();
		}
	});
}

function updateSum() {
	$("input[type=checkbox][name=all]").each(function() {
		var me = $(this);
		$("input[type=checkbox][name='" + me.val() + "']").each(function() {
			var o = $(this);
			var n = strToFloat($("input[id^='num_'][code="+o.attr("code")+"]").val(), 1);
			var ns = parseFloat($("#"+me.val()+"_"+o.val()).val()) * n;
			$("#sum_"+o.attr("code")).text(ns);
		});
	});
}

function initState() {
	$("input[type=checkbox][name=all]").each(function() {
		var o = $(this),
		len = $("input[type=checkbox][name='" + o.val() + "']").length,
		slen = $("input[type=checkbox][name='" + o.val() + "']:checked").length;
		o.attr("checked", !(len > slen));
	});
	
	var alen = $("input[type=checkbox][name!=allall]").length,
	salen = $("input[type=checkbox][name!=allall]:checked").length;
	$("input[type=checkbox][name=allall]").attr("checked", (alen == salen));
}

function initStatistical() {
	var total = 0;
	$("input[type=checkbox][name=all]").each(function() {
		var me = $(this);
		var sum = 0;
		$("input[type=checkbox][name='" + me.val() + "']:checked").each(function() {
			var o = $(this);
			var n = strToFloat($("input[id^='num_'][code="+o.attr("code")+"]").val(), 1);
			var ns = parseFloat($("#"+me.val()+"_"+o.val()).val()) * n;
			sum += ns;
		});
		$("span ._price[dp="+me.val()+"]").text(sum);
		total += sum;
	});
	$("#cart_price").text("￥" + total);
	$("#cart_count").text($("input[type=checkbox][name!=allall][name!=all]:checked").length);
}

function changeNum(o) {
	init();
	saveNum($(o).attr("code"), strToInteger($(o).val(), 1));
}

function saveNum(id, n) {
	AjaxUtil.command.executeUpdate("sjc_cart_7", {goods_id:id, num:n});
}

function strToFloat(str, defVal) {
	if(!str) return defVal;
	var fRet = parseFloat(str);
	if(isNaN(fRet)){
		if(defVal){
			fRet = defVal;
		}
		else{
			fRet = 0;
		}
	}
	return fRet;
}

function strToInteger(str, defVal) {
	if(!str) return defVal;
	var iRet = parseInt(str);
	if(isNaN(iRet)){
		if(defVal){
			iRet = defVal;
		}
		else{
			iRet = 0;
		}
	}
	return iRet;
}